;; Exercise 1.3.  Define a procedure that takes three numbers as arguments and r
;; eturns the sum of the squares of the two larger numbers.
;;  


;; utilizando gambit/chicken
;; r5rs
(define (sqrt+2 a b c)
  (define (sum-square a b)
    (+ (* a a) (* b b)))
  (let ((menor (min a b c)))
    (cond ((= a menor) (sum-square b c))
          ((= b menor) (sum-square a c))
          (else (sum-square a b)))))

;; utilizando ypsilon/ikarus
;; r6rs
(define (sqrt+2 a b c)
  (define (square a)
    (* a a))
  (define (sum-square l)
    (+ (square (car l)) (square (cadr l))))
  (sum-square
   (cdr (list-sort < (list a b c)))))

;; testes
;; utilizando ypsilon
(begin
  (import (test-lite))
  (test-begin ">> SICP")
  (test-comment "Dados três números, calcular o quadrado dos dois maiores.")
  (test-equal (sqrt+2 1 1 1)    2)
  (test-equal (sqrt+2 2 1 1)    5)
  (test-equal (sqrt+2 1 2 1)    5)
  (test-equal (sqrt+2 1 1 2)    5)
  (test-equal (sqrt+2 0 0 1)    1)
  (test-equal (sqrt+2 3 -3 -3) 18)
  (test-equal (sqrt+2 2 3 7)   58)
  (test-equal (sqrt+2 1 1 1)    2)
  (test-equal (sqrt+2 2 3 4)   25)
  (test-end))

;; >> SICP
;; comment: "Dados três números, calcular o quadrado dos dois maiores."
;; Passed 9